﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VOD.Models;
using System.Web.Helpers;
using VOD.ViewModels;
using VOD.Helpers;
using System.Data.Entity;
using System.Web.Security;

namespace VOD.Controllers
{
    public class TestController : Controller
    {
        VODEntities db = new VODEntities();
        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Test/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Test/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Test/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Test/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Test/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Test/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Test/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult TypeList()
        {
            var types = db.VideoTypes.Include("VideoCategory").Where(t => t.VideoCategory.CategoryID == 1).ToList();
            return View(types);
        }

        public ActionResult CatogoryDetails()
        {
            var catogory = db.VideoTypes.Single(t => t.TypeName == "动作").VideoCategory;
            return View(catogory);
        }

        public string UserDetails()
        {
            return null;
        }

        public ActionResult JqueryStudy()
        {
            return View();
        }

        public ActionResult UploadImg()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UploadImg(FormCollection collection)
        {
            WebImage pic = WebImage.GetImageFromRequest("PicUrl");
            ViewBag.pic = pic;
            //var file = Request.Files["file1"];
            //WebImage pic = WebImage.GetImageFromRequest(file);
            return View();
        }

        public ActionResult LangList()
        {
            var langs = db.VideoLanguages.ToList();
            return View(langs);
        }

        public ActionResult LangDetails(int id)
        {
            var lang = db.VideoLanguages.Find(id);
            return View(lang);
        }

        public class cFileList
        {
            public int partID { get; set; }
            public int orderNum { get; set; }
        }

        public string Json()
        {
            List<cFileList> list= new List<cFileList>{
                new cFileList{
                    partID=3,
                    orderNum=1
                },
                new cFileList{
                    partID=4,
                    orderNum=2
                }
            };
            var json = JsonHelper.JsonSerializer<List<cFileList>>(list);
            List<cFileList> list2 = JsonHelper.JsonDeserialize<List<cFileList>>(json);
            return list[0].orderNum.ToString();
        }

        public ActionResult WebGrid()
        {
            var videos = db.Videos.ToList();
            return View(videos);
        }

        public ActionResult Chart(string s)
        {

            var counts = (from c in db.SiteCounts
                          group c by c.IP_Addr into g
                          select new SiteCounterChart()
                          {
                              IP_Addr = g.Key,
                              Counts = g.Count()
                          }).OrderByDescending(c => c.Counts).Take(10);
            return View(counts);
        }

        public ActionResult Play()
        {
            return View();
        }

        public ActionResult FlexiGrid()
        {
            return View();
        }

        public class JsonData
        {
            public int page { get; set; }
            public int total { get; set; }
        }

        public ActionResult TestJson()
        {
            var videos = db.v_Videos.ToList();

            var data = new
            {
                page = 1,
                total = 2,
                rows = videos
            };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public ActionResult VideoTypes()
        {
            var videos = db.Videos
                .Include(v => v.VideoAndTypes.Select(t => t.VideoType));
            return View(videos);
        }

        public ActionResult AutoCompleteJson(string term=null)
        {
            var videos = db.Videos
                .Take(10)
                .Select(r => new { label = r.Name });
            if (term!=null)
            {
                videos = videos.Where(v => v.label.Contains(term));
            }
            return Json(videos, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AutoCompleteView()
        {
            return View();
        }

        public ActionResult FileUpload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult FileUpload(FormCollection collection)
        {
            var file = Request.Files["file"];
            var path=Server.MapPath("~/Content/11.txt");
            file.SaveAs(path);
            return View();
        }

        public String FileName()
        {
            var path = DateTime.Now.ToString("yyyy-MM");
            var fileName = path+"/"+DateTime.Now.Ticks.ToString() + ".docx";
            return fileName;
        }

        public ActionResult BlockUI()
        {
            return View();
        }

        public ActionResult Ajax()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Ajax(string txt)
        {
            if (Request.IsAjaxRequest())
            {
                return Content("Test "+txt);
            }
            return View();
        }

        public ActionResult Time()
        {
            return View();
        }

        public ActionResult ServerPath()
        {
            return Content(Url.Content("~"));
        }

        public ActionResult ResetPwd()
        {
            MembershipUser user = Membership.GetUser("admin");
            string password = user.ResetPassword();
            user.ChangePassword(password, "guan00");
            return Content(password);
        }
    }
}
